home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / IRC client Source / ircle sources / InputLine.p < prev    next >
Encoding:
Text File  |  1993-05-21  |  8.1 KB  |  405 lines  |  [TEXT/PJMM]

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: InputLine    }
  3. {    Copyright © 1992 Olaf Titz (s_titz@ira.uka.de)    }
  4.  
  5. {    This program is free software; you can redistribute it and/or modify    }
  6. {    it under the terms of the GNU General Public License as published by    }
  7. {    the Free Software Foundation; either version 2 of the License, or    }
  8. {    (at your option) any later version.    }
  9.  
  10. {    This program is distributed in the hope that it will be useful,    }
  11. {    but WITHOUT ANY WARRANTY; without even the implied warranty of    }
  12. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    }
  13. {    GNU General Public License for more details.    }
  14.  
  15. {    You should have received a copy of the GNU General Public License    }
  16. {    along with this program; if not, write to the Free Software    }
  17. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.    }
  18.  
  19. unit InputLine;
  20. { Provides a small window with status and input lines. }
  21. { All keystrokes go into the input line. Implements a command history.}
  22.  
  23. interface
  24. uses
  25.     ApplBase;
  26.  
  27. procedure InitInputLine;
  28. { Startup }
  29.  
  30. procedure OpenInputLine (process: ProcPtr);
  31. { Open the input line window }
  32. { process(var s:string) gets called whenever Return was pressed }
  33.  
  34. procedure SetInputLine (var s: string);
  35. { Preset the input line }
  36.  
  37. procedure InsertInputLine (var s: string);
  38. { Insert a string into the input line }
  39.  
  40. procedure StatusLine (var s: string);
  41. { Set the status line }
  42.  
  43. procedure CloseInputLine;
  44. { Close the window }
  45.  
  46. implementation
  47.  
  48. const
  49.     MAXHIST = 5000;    { Maximum # of chars to store in command history }
  50.     MAXLINE = 240;        { Maximum length of input line }
  51.  
  52. var
  53.     iw: WindowPtr;
  54.     Hact, Hupd, Hmouse, Hkey, Hakey, Hidle, Hpaste: integer;
  55.     status: string[80];
  56.     lineh: TEHandle;
  57.     procs: ProcPtr;
  58.     line1, line2, letterw: integer;
  59.     ReturnHit: boolean;
  60.     hist: CharsHandle;
  61.     hpos: integer;
  62.  
  63. procedure initInputLine;
  64.     var
  65.         i: integer;
  66.     begin
  67.         iw := nil;
  68.         lineh := nil;
  69.         hist := CharsHandle(NewHandle(1));
  70.         hist^^[0] := chr(0);
  71.         hpos := 0;
  72.     end;
  73.  
  74. procedure DoRedraw (l: integer);
  75.     var
  76.         p0: GrafPtr;
  77.     begin
  78.         GetPort(p0);
  79.         SetPort(iw);
  80.         if l = 1 then begin
  81.             MoveTo(1, line1);
  82.             DrawString(status);
  83.         end
  84.         else begin
  85.             TEUpdate(lineh^^.viewRect, lineh);
  86.         end;
  87.         SetPort(p0);
  88.     end;
  89.  
  90. procedure StackupLine (var line: string);
  91.     var
  92.         i: integer;
  93.     begin
  94.         if gethandlesize(Handle(hist)) > MAXHIST then begin
  95.             i := 1;
  96.             while hist^^[i] <> chr(0) do
  97.                 i := succ(i);
  98.             i := Munger(Handle(hist), 0, nil, i, ptr(1), 0);
  99.         end;
  100.         i := length(line) + 1;
  101.         if i > 1 then begin
  102.             line[i] := chr(0);
  103.             i := PtrAndHand(@line[1], Handle(hist), i);
  104.             hpos := gethandlesize(Handle(hist)) - 1;
  105.         end
  106.     end;
  107.  
  108. procedure RecallLine (p: integer);
  109.     var
  110.         i: integer;
  111.         s: string;
  112.     begin
  113.         hpos := p;
  114.         i := 0;
  115.         repeat
  116.             p := succ(p);
  117.             i := succ(i);
  118.             s[i] := hist^^[p];
  119.         until s[i] = chr(0);
  120.         s[0] := chr(i - 1);
  121.         SetInputLine(s);
  122.     end;
  123.  
  124. procedure RecallLineUp;
  125.     var
  126.         i: integer;
  127.     begin
  128.         i := hpos;
  129.         if i > 0 then begin
  130.             repeat
  131.                 i := pred(i)
  132.             until hist^^[i] = chr(0);
  133.             RecallLine(i);
  134.         end
  135.     end;
  136.  
  137. procedure RecallLineDown;
  138.     var
  139.         i: integer;
  140.         s: string[1];
  141.     begin
  142.         i := hpos;
  143.         if i < gethandlesize(handle(hist)) then begin
  144.             repeat
  145.                 i := succ(i)
  146.             until hist^^[i] = chr(0);
  147.             if i < gethandlesize(handle(hist)) then
  148.                 RecallLine(i)
  149.             else begin
  150.                 s := '';
  151.                 SetInputLine(s);
  152.             end;
  153.         end
  154.         else begin
  155.             s := '';
  156.             SetInputLine(s);
  157.         end
  158.     end;
  159.  
  160.  
  161. procedure SetCursor (n: integer);
  162.     begin
  163.         if n < 1 then
  164.             n := 1
  165.         else if n > MAXLINE then
  166.             n := MAXLINE;
  167.         TESetSelect(n, n, lineh);
  168.         DoRedraw(2);
  169.     end;
  170.  
  171.  
  172. function Activate (var e: EventRecord): boolean;
  173.     begin
  174.         if iw <> nil then
  175.             if bitand(e.message, 1) = 1 then begin
  176.                 ShowWindow(iw);
  177.                 TEActivate(lineh)
  178.             end
  179.             else begin
  180.                 TEDeActivate(lineh);
  181.                 HideWindow(iw);
  182.             end;
  183.         Activate := false
  184.     end;
  185.  
  186. function Update (var e: EventRecord): boolean;
  187.     begin
  188.         if WindowPtr(e.message) = iw then begin
  189.             BeginUpdate(iw);
  190.             MoveTo(1, line1);
  191.             DrawString(status);
  192.             TEUpdate(lineh^^.viewRect, lineh);
  193.             EndUpdate(iw);
  194.             Update := true
  195.         end
  196.         else
  197.             Update := false
  198.     end;
  199.  
  200. function Mouse (var e: EventRecord): boolean;
  201.     begin
  202.         if WindowPtr(e.message) = iw then begin
  203.             GlobalToLocal(e.where);
  204.             if e.where.v < 11 then begin
  205.                 e.what := mouseMsg + inDrag;
  206.                 LocalToGlobal(e.where);
  207.                 ApplEvents(e);  { Let ApplBase do the dragging }
  208.             end
  209.             else begin
  210.                 TEClick(e.where, false, lineh);
  211.             end;
  212.             Mouse := true
  213.         end
  214.         else
  215.             Mouse := false;
  216.     end;
  217.  
  218. procedure SCALL (var s: string; p: ProcPtr);
  219. inline
  220.     $205F, $4E90;        { movea.l (a7)+,a0; jsr (a0) }
  221.  
  222. procedure GotLine;
  223.     var
  224.         i: integer;
  225.         c: string[1];
  226.         line: string;
  227.     begin
  228.         ReturnHit := true;
  229.         i := lineh^^.teLength;
  230.         if i > 255 then
  231.             i := 255;
  232.         BlockMove(lineh^^.htext^, @line[1], i);
  233.         while (i > 0) and (line[i] = ' ') do
  234.             i := pred(i);
  235.         line[0] := chr(i);
  236.         StackupLine(line);
  237.         c := '';
  238.         SetInputLine(c);
  239.         SCALL(line, procs);
  240.         ReturnHit := false;
  241.     end;
  242.  
  243.  
  244. function Key (var e: EventRecord): boolean;
  245.     var
  246.         c: char;
  247.         i: integer;
  248.         p0: GrafPtr;
  249.     begin
  250.         if iw = nil then
  251.             Key := false
  252.         else if not ReturnHit then begin
  253.             getPort(p0);
  254.             SetPort(iw);
  255.             c := chr(e.message mod 256);
  256.             case ord(c) of
  257.                 13: 
  258.                     GotLine;
  259.                 30: 
  260.                     RecallLineUp;
  261.                 31: 
  262.                     RecallLineDown;
  263.                 otherwise
  264.                     begin
  265.                     TEKey(c, lineh);
  266.                     TESelView(lineh)
  267.                 end;
  268.             end;
  269.             SetPort(p0);
  270.         end;
  271.         Key := true;
  272.     end;
  273.  
  274. function AKey (var e: EventRecord): boolean;
  275.     begin
  276.         AKey := Key(e)
  277.     end;
  278.  
  279. function Idle (var e: EventRecord): boolean;
  280.     begin
  281.         TEIdle(lineh);
  282.         Idle := false;
  283.     end;
  284.  
  285. function Paste (var e: EventRecord): boolean;
  286.     var
  287.         h: CharsHandle;
  288.         i, n, c: integer;
  289.         f: EventRecord;
  290.         b: boolean;
  291.     begin
  292.         if e.message = 5 then begin
  293.             i := TEFromScrap;
  294.             h := CharsHandle(TEScrapHandle);
  295.             n := TEGetScrapLen;
  296.             for i := 0 to n - 1 do begin
  297.                 c := ord(h^^[i]);
  298.                 f.message := c;
  299.                 b := Key(f);
  300.                 if c = 13 then
  301.                     repeat
  302.                         ApplRun
  303.                     until not ReturnHit;
  304.             end;
  305.             Paste := true
  306.         end
  307.         else
  308.             Paste := false
  309.     end;
  310.  
  311. procedure OpenInputLine (process: ProcPtr);
  312.     var
  313.         p0: GrafPtr;
  314.         fi: FontInfo;
  315.         r: Rect;
  316.         i: integer;
  317.     begin
  318.         if iw = nil then begin
  319.             for i := 1 to 80 do begin
  320.                 Status[i] := ' ';
  321.             end;
  322.             ReturnHit := false;
  323.             Status[0] := chr(80);
  324.             SetRect(r, 0, 0, 16, 16);
  325.             iw := NewWindow(nil, r, '', false, 3, WindowPtr(-1), false, 0);
  326.             if iw <> nil then begin
  327.                 GetPort(p0);
  328.                 SetPort(iw);
  329.                 SetOrigin(-2, -2);
  330.                 penMode(patXor);
  331.                 TextFont(monaco);
  332.                 TextSize(9);
  333.                 TextFace([]);
  334.                 TextMode(srcCopy);
  335.                 GetFontInfo(fi);
  336.                 line1 := fi.ascent + fi.leading;
  337.                 line2 := line1 + fi.descent + fi.leading + fi.ascent + 1;
  338.                 letterw := fi.widMax;
  339.                 SizeWindow(iw, 80 * letterw + 4, line2 + fi.descent + fi.leading + 6, true);
  340.                 with screenBits.bounds do
  341.                     MoveWindow(iw, (right - left - iw^.portRect.right + 2) div 2 - 1, bottom - iw^.portRect.bottom - 5, true);
  342.                 SetRect(r, 0, line1 + fi.descent + 1, 80 * letterw, line2 + fi.descent);
  343.                 lineh := TENew(r, r);
  344.                 if lineh <> nil then begin
  345.                     Hact := ApplTask(@Activate, app4Evt);
  346.                     Hupd := ApplTask(@Update, updateEvt);
  347.                     Hmouse := ApplTask(@Mouse, mouseMsg + inContent);
  348.                     Hkey := ApplTask(@Key, keyDown);
  349.                     Hakey := ApplTask(@Akey, autoKey);
  350.                     Hidle := ApplTask(@Idle, nullEvent);
  351.                     Hpaste := ApplTask(@Paste, menuMsg + editMenu);
  352.                     SetPort(p0);
  353.                     ShowWindow(iw);
  354.                     TEAutoView(true, lineh);
  355.                     TEActivate(lineh);
  356.                     procs := process;
  357.                 end;
  358.             end
  359.         end;
  360.     end;
  361.  
  362. procedure StatusLine (var s: string);
  363.     begin
  364.         status := s;
  365.         DoRedraw(1);
  366.     end;
  367.  
  368.  
  369. procedure InsertInputLine (var s: string);
  370.     var
  371.         i: integer;
  372.         f: EventRecord;
  373.         b: boolean;
  374.     begin
  375.         TEInsert(@s[1], length(s), lineh);
  376.         TESelView(lineh);
  377.     end;
  378.  
  379. procedure SetInputLine (var s: string);
  380.     begin
  381.         if iw <> nil then begin
  382.             TEDeactivate(lineh);
  383.             TESetSelect(0, 32767, lineh);
  384.             TEDelete(lineh);
  385.             InsertInputLine(s);
  386.             SetCursor(length(s) + 1);
  387.             TESelView(lineh);
  388.             TEActivate(lineh);
  389.         end
  390.     end;
  391.  
  392. procedure CloseInputLine;
  393.     begin
  394.         ApplUntask(Hact);
  395.         ApplUntask(Hupd);
  396.         ApplUntask(Hmouse);
  397.         ApplUntask(Hkey);
  398.         ApplUntask(Hakey);
  399.         ApplUntask(Hidle);
  400.         ApplUntask(Hpaste);
  401.         DisposeWindow(iw);
  402.         iw := nil
  403.     end;
  404.  
  405. end.